PSPSDK=$(shell psp-config --pspsdk-path)
PSPDEV=$(shell psp-config --pspdev-path)
INCLUDE=$(PSPSDK)/include

# default : dev
# make distrib to compile without debug info
# make or make dev to compile with debug info
all: base
nonids: base
base: hbl loader

clean:
	rm -rf *~ *.o */*.o exploits/*/*.o *.elf *.BIN *.s */*.s

# use a different FOLDER to make for different exploits
# Exploit-specific files go in the subfolders, see targets hbl and loader below
FOLDER=wop

ifeq ($(wildcard exploits/$(FOLDER)/exploit_config.c),) 
    EXPLOIT_CONFIG=config
else 
    EXPLOIT_CONFIG=exploits/$(FOLDER)
endif

CC       = psp-gcc
ASM      = psp-as

CFLAGS   := -D PSP -I $(INCLUDE) -I exploits/$(FOLDER) -I . -Os -W -Wall -Werror -Wshadow -G0 -fno-pic -mno-abicalls -fomit-frame-pointer


all: CFLAGS += -D DEBUG -D NID_DEBUG
nonids: CFLAGS += -D DEBUG
dev: all
distrib: base

OBJS_COMMON = \
	debug/debug.o \
	common/globals.o \
	common/lib.o \
	common/utils.o \
	config/config.o \
	eloader/runtime_stubs.o \
	memory/malloc.o \
	$(EXPLOIT_CONFIG)/exploit_config.o

OBJS_HBL = $(OBJS_COMMON) \
	eloader/eloader.o \
	eloader/tables.o \
	eloader/hook.o \
	eloader/resolve.o \
	common/md5.o \
	common/test.o \
	elf/elf.o \
	elf/modmgr.o \
	memory/memory.o \
	memory/reloc.o \
	config/settings.o \
	graphics/graphics.o \
	graphics/font.o \
	exploits/$(FOLDER)/sdk_hbl.o

OBJS_LOADER = $(OBJS_COMMON) \
	loader.o \
	exploits/$(FOLDER)/sdk_loader.o

# Files to compile

hook.o: hook.c
	$(CC) $(CFLAGS) -S eloader/hook.c -o eloader/hook.s
	$(ASM) eloader/hook.s -o eloader/hook.o

resolve.o: resolve.c
	$(CC) $(CFLAGS) -S eloader/resolve.c -o eloader/resolve.s
	$(ASM) eloader/resolve.s -o eloader/resolve.o

runtime_stubs.o: runtime_stubs.c
	$(CC) $(CFLAGS) -S eloader/runtime_stubs.c -o eloader/runtime_stubs.s
	$(ASM) eloader/runtime_stubs.s -o eloader/runtime_stubs.o

tables.o: tables.c
	$(CC) $(CFLAGS) -S eloader/tables.c -o eloader/tables.s
	$(ASM) eloader/tables.s -o eloader/tables.o

globals.o: globals.c
	$(CC) $(CFLAGS) -S common/globals.c -o common/globals.s
	$(ASM) common/globals.s -o common/globals.o

lib.o: lib.c
	$(CC) $(CFLAGS) -S common/lib.c -o common/lib.s
	$(ASM) common/lib.s -o common/lib.o

md5.o: md5.c
	$(CC) $(CFLAGS) -S common/md5.c -o common/md5.s
	$(ASM) common/md5.s -o common/md5.o

modmgr.o: modmgr.c
	$(CC) $(CFLAGS) -S elf/modmgr.c -o elf/modmgr.s
	$(ASM) elf/modmgr.s -o elf/modmgr.o

utils.o: utils.c
	$(CC) $(CFLAGS) -S common/utils.c -o common/utils.s
	$(ASM) common/utils.s -o common/utils.o

config.o: config.c
	$(CC) $(CFLAGS) -S config/config.c -o config/config.s
	$(ASM) config/config.s -o config/config.o

sdk_hbl.o: sdk_hbl.S
	$(ASM) exploits/$(FOLDER)/sdk_hbl.S -o exploits/$(FOLDER)/sdk_hbl.o

sdk_loader.o: sdk_loader.S
	$(ASM) exploits/$(FOLDER)/sdk_loader.S -o exploits/$(FOLDER)/sdk_loader.o

exploit_config.o: exploit_config.c
	$(CC) $(CFLAGS) -S $(EXPLOIT_CONFIG)/exploit_config.c -o $(EXPLOIT_CONFIG)/exploit_config.s
	$(ASM) $(EXPLOIT_CONFIG)/exploit_config.s -o $(EXPLOIT_CONFIG)/exploit_config.o

settings.o: settings.c
	$(CC) $(CFLAGS) -S config/settings.c -o config/settings.s
	$(ASM) config/settings.s -o config/settings.o

debug.o: debug.c
	$(CC) $(CFLAGS) -S debug/debug.c -o debug/debug.s
	$(ASM) debug/debug.s -o debug/debug.o

elf.o: elf.c
	$(CC) $(CFLAGS) -S elf/elf.c -o elf/elf.s
	$(ASM) elf/elf.s -o elf/elf.o

graphics.o: graphics.c
	$(CC) $(CFLAGS) -S graphics/graphics.c -o graphics/graphics.s
	$(ASM) graphics/graphics.s -o graphics/graphics.o

font.o: font.c
	$(CC) $(CFLAGS) -S graphics/font.c -o graphics/font.s
	$(ASM) graphics/font.s -o graphics/font.o

memory.o: memory.c
	$(CC) $(CFLAGS) -S memory/memory.c -o memory/memory.s
	$(ASM) memory/memory.s -o memory/memory.o

malloc.o: malloc.c
	$(CC) $(CFLAGS) -S memory/malloc.c -o memory/malloc.s
	$(ASM) memory/malloc.s -o memory/malloc.o

reloc.o: reloc.c
	$(CC) $(CFLAGS) -S memory/reloc.c -o memory/reloc.s
	$(ASM) memory/reloc.s -o memory/reloc.o

common/test.o: common/test.c
	$(CC) $(CFLAGS) -S common/test.c -o common/test.s
	$(ASM) common/test.s -o common/test.o

eloader.o: eloader.c
	$(CC) $(CFLAGS) -S eloader/eloader.c -o eloader/eloader.s
	$(ASM) eloader/eloader.s -o eloader/eloader.o

loader.o: loader.c
	$(CC) $(CFLAGS) -S loader.c -o loader.s
	$(ASM) loader.s -o loader.o

#END


# H.BIN and HBL.BIN
hbl: $(OBJS_HBL) exploits/$(FOLDER)/linker_hbl.x
	$(PSPDEV)/bin/psp-ld -T exploits/$(FOLDER)/linker_hbl.x -L$(PSPSDK)/lib $(OBJS_HBL) -o eloader.elf
	$(PSPDEV)/bin/psp-strip -s eloader.elf
	$(PSPDEV)/bin/psp-objcopy -O binary eloader.elf HBL.BIN
	
loader: $(OBJS_LOADER) exploits/$(FOLDER)/linker_loader.x
	$(PSPDEV)/bin/psp-ld -T exploits/$(FOLDER)/linker_loader.x -L$(PSPSDK)/lib  $(OBJS_LOADER) -o loader.elf
	$(PSPDEV)/bin/psp-strip -s loader.elf
	$(PSPDEV)/bin/psp-objcopy -O binary loader.elf H.BIN
